/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    getList,
    createMarketActivity,
    updateMarketActivity,
    deleteMarketActivity,
    switchState
} from '../../api/modules/market_activity'

const state = {
    market_activities_list: [],
    showLoading: false,
    total: 100,
    state_list: [{id:0,name:'待审'},{id:1,name:'审核通过'},{id:2,name:'审核不通过'},{id:-1,name:'结束'}],
    type_list: [{id:1,name:'物资'},{id:2,name:'讲座'},{id:3,name:'培训'},{id:4,name:'广告'},{id:5,name:'其他'}],
}

// getters
// actions
const actions = {
    get_market_activities_list({
            commit
        }, param) {
        commit(types.MARKET_ACTIVITIES_SHOW_LOADING, param);
        getList(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.MARKET_ACTIVITIES_LIST_LOADED, res.data.msg);
            }
        });
    },
    add_market_activities({
            commit
        }, param) {
        commit(types.MARKET_ACTIVITIES_SHOW_LOADING, param);
        createMarketActivity(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.MARKET_ACTIVITIES_ADDED, { result: res.data.msg, data: param });
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_market_activities({
            commit
        }, param) {
        commit(types.MARKET_ACTIVITIES_SHOW_LOADING, param);
        updateMarketActivity(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.MARKET_ACTIVITIES_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    delete_market_activities({
            commit
        }, param) {
        commit(types.MARKET_ACTIVITIES_SHOW_LOADING, param);
        deleteMarketActivity(param).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.MARKET_ACTIVITIES_DELETED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    update_market_activities_state({
            commit
        }, param) {
        commit(types.MARKET_ACTIVITIES_SHOW_LOADING, param);
        switchState(param.id, param.value).then((res) => {
            if (res.data.res_code === 1) {
                commit(types.MARKET_ACTIVITIES_UPDATED, param);
                if (param._fn)
                    param._fn();
            }
        });
    },
    clear_market_activities({commit}) {
        commit(types.MARKET_ACTIVITIES_CLEAR);
    }
}
// mutations
const mutations = {
    [types.MARKET_ACTIVITIES_SHOW_LOADING](state, param) {
        state.showLoading = true;
    },
    [types.MARKET_ACTIVITIES_CLEAR](state) {
        state.market_activities_list = [];
    },
    [types.MARKET_ACTIVITIES_LIST_LOADED](state, param) {
        // state.market_activities_list = param.list;
        state.market_activities_list = param;
        state.total = param.total;
        state.showLoading = false;
    },
    [types.MARKET_ACTIVITIES_ADDED](state, param) {
        state.market_activities_list.push({
            id: param.result,
            market_activity_id: param.result,
            dealer_id: param.data.dealer_id,
            sale_user_id: param.data.sale_user_id,
            expect_start_time: param.data.expect_start_time,
            start_time: param.data.start_time,
            type: param.data.type,
            description: param.data.description,
            expect_amount: param.data.expect_amount,
            amount: param.data.amount,
            create_time:new Date(),
            state: 0
        })
        state.showLoading = false;
    },
    [types.MARKET_ACTIVITIES_UPDATED](state, param) {
        for (var i = 0; i < state.market_activities_list.length; i++) {
            if (state.market_activities_list[i].id === param.id) {
                if (!param.hasOwnProperty('value')) {
                    for(var p in param)
                    {
                        state.market_activities_list[i][p] = param[p]
                    }
                }
                else {
                    state.market_activities_list[i].state = param.value;
                }
            }
        }
        state.showLoading = false;
    },
    [types.MARKET_ACTIVITIES_DELETED](state, param) {
        for (var i = 0; i < state.market_activities_list.length; i++) {
            if (state.market_activities_list[i].id === param) {
                state.market_activities_list.splice(i, 1);
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
